package org.darkforest.admin.controller;

import java.io.File;
import java.io.IOException;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.common.collect.Multimap;
import org.apache.commons.collections.map.MultiValueMap;
import org.apache.commons.lang3.StringUtils;
import org.darkforest.annotation.ArchivesLog;
import org.darkforest.common.services.ActivityService;
import org.darkforest.common.services.ActivitySignService;
import org.darkforest.common.util.AliOssFileUploadUtil;
import org.darkforest.common.util.DateUtil;
import org.darkforest.common.util.TreeListUtil;
import org.darkforest.constant.Constant;
import org.darkforest.controller.WebController;
import org.darkforest.dao.mapper.ActivityGiftMapper;
import org.darkforest.dao.mapper.ActivitySignMapper;
import org.darkforest.dao.mapper.ActivityMapper;
import org.darkforest.dao.mapper.MemberMapper;
import org.darkforest.model.*;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("operation")
public class ActivityController extends WebController<ActivityController> {

    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private ActivityGiftMapper activityGiftMapper;

    @Autowired
    private ActivitySignMapper activitySignMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private ActivityService activityService;

    @Autowired
    private ActivitySignService activitySignService;


    /**
     * 活动列表
     *
     * @param request
     * @return
     */
    @RequestMapping("activity")
    public ModelAndView activity(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView("operation/activity/list");

        // 检索条件
        String activityTitle = request.getParameter("activityTitle");
        String activityType = request.getParameter("activityType");
        // 改页处理用
        String start = request.getParameter("start");

        Activity activity = new Activity();
        if (activityType != null && !activityType.equals("")) {
            activity.setActivityType(activityType);
        }
        activity.setActivityTitle(activityTitle);
        // 改页处理时
        if (start != null && !"".equals(start)) {
            activity.setStart(Integer.valueOf(start));
        }

        activity.setResultList(activityMapper.selectListByCondition(activity));
        modelAndView.addObject("page", activity);

        return modelAndView;
    }

    /**
     * 活动增加
     *
     * @return
     */
    @RequestMapping("activity/toAdd")
    public ModelAndView articleAdd() {
        ModelAndView modelAndView = new ModelAndView("operation/activity/add");
        return modelAndView;
    }

    @RequestMapping("activity/doAdd")
    @ArchivesLog(operationDescription = "新增活动")
    @ResponseBody
    public AjaxReturnData doAddActivity(Activity activity) {

        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        // 签到活动只能有一个
        if (Constant.ACTIVITY_TYPE_SIGN.equals(activity.getActivityType())) {
            Activity activityTemp = new Activity();
            activityTemp.setActivityType(Constant.ACTIVITY_TYPE_SIGN);
            List<Activity>  activityList = activityMapper.selectListByCondition(activityTemp);
            if (activityList.size() > 0) {
                ajaxReturnData.setResult(false);
                ajaxReturnData.setMessage("签到活动只能有一个");
                return ajaxReturnData;
            }
        }

        int i = activityService.insertActivity(activity);
        if (i > 0) {
            ajaxReturnData.setResult(true);
        } else {
            ajaxReturnData.setResult(false);
        }

        return ajaxReturnData;
    }

    /**
     * 活动修改
     *
     * @param activityId
     * @return
     */
    @RequestMapping("activity/toModify")
    public ModelAndView toModifyActivity(Integer activityId) {
        ModelAndView modelAndView = new ModelAndView("operation/activity/edit");
        Activity activity = activityMapper.selectByPrimaryKey(activityId);
        modelAndView.addObject("activity", activity);
        ActivityGift activityGift = new ActivityGift();
        activityGift.setActivityId(activityId);
        List<ActivityGift> activityGiftList = activityGiftMapper.selectListByCondition(activityGift);
        modelAndView.addObject("activityGiftList", activityGiftList);
        return modelAndView;
    }

    @RequestMapping("activity/doModify")
    @ArchivesLog(operationDescription = "修改活动")
    public @ResponseBody boolean doModifyActivity(HttpServletRequest request, Activity activity) {

        // 礼品的修改
        String editGiftInfo = request.getParameter("editGiftInfo");

        int i = activityService.updateActivity(activity, editGiftInfo);
        if (i ==  0) {
            return false;
        }

        return true;
    }

    /**
     *  奖品的删除
     * @param giftId
     * @return
     */
    @RequestMapping(value = "activity/delGift")
    public @ResponseBody boolean delGift(Integer giftId){
        int delCount = activityGiftMapper.deleteByPrimaryKey(giftId);
        // 删除件数不是一件时
        if (delCount != 1) {
            return false;
        }
        return true;
    }

    /**
     * 活动删除
     *
     * @param request
     * @return
     */
    @RequestMapping("activity/delete")
    @ArchivesLog(operationDescription = "删除活动")
    public ModelAndView delActivity(HttpServletRequest request) {
        String[] activityId = request.getParameterValues("activityId");

        ModelAndView modelAndView = new ModelAndView("msg");

        int i = activityService.deleteActivity(activityId);
        if (i ==  1) {
            modelAndView.addObject("msg", "活动删除成功");
        } else {
            modelAndView.addObject("msg", "活动删除失败");
        }
        modelAndView.addObject("returnUrl", "operation/activity");
        return modelAndView;
    }

    /**
     * 排行榜列表
     *
     * @param activityId
     * @return
     */
    @RequestMapping("activity/signDetail")
    public ModelAndView signDetail(HttpServletRequest request,Integer activityId) {
        ModelAndView modelAndView = new ModelAndView("operation/activity/signDetail");

        if(StringUtils.isNotEmpty(request.getParameter("activityId"))){
            activityId = Integer.valueOf(request.getParameter("activityId"));
        }

        ActivitySign signsRecord = new ActivitySign();
        signsRecord.setActivityId(activityId);
        //检索条件
        String memberPhone = request.getParameter("memberMobile");
        if(StringUtils.isNotEmpty(memberPhone)){
            Member member = new Member();
            member.setMemberMobile(memberPhone);

            List<Member> memberList = memberMapper.getMemberByCond(member);
            if ((memberList != null) && (memberList.size() > 0)) {
                Member memberQ = memberMapper.getMemberByCond(member).get(0);
                signsRecord.setMemberId(memberQ.getMemberId());
            } else {
                signsRecord.setMemberId(0);
            }
            // 页面显示用
            signsRecord.setMemberMobile(memberPhone);
        }

        // 需要分页处理时的条件设定
        String start = request.getParameter("start");
        // 改页处理时
        if (start != null && !"".equals(start)) {
            signsRecord.setStart(Integer.valueOf(start));
        }

        List<ActivitySign> activitySignList = activitySignMapper.selectLastSignList(signsRecord);

        // 用户手机号的取得
        for (ActivitySign activitySign: activitySignList) {
            MemberWithBLOBs memberInfo = memberMapper.selectByPrimaryKey(activitySign.getMemberId());
            // 显示手机号
            String memberMobile = memberInfo.getMemberMobile();
            activitySign.setMemberMobile(memberMobile);
        }
        signsRecord.setResultList(activitySignList);
        modelAndView.addObject("page", signsRecord);
        return modelAndView;
    }

    /**
     * 中奖纪录
     *
     * @param activityId
     * @return
     */
    @RequestMapping("activity/rewardDetail")
    public ModelAndView rewardDetail(HttpServletRequest request,Integer activityId,Integer memberId) {
        ModelAndView modelAndView = new ModelAndView("operation/activity/rewardDetail");

        if(StringUtils.isNotEmpty(request.getParameter("activityId"))){
            activityId = Integer.valueOf(request.getParameter("activityId"));
        }
        if(StringUtils.isNotEmpty(request.getParameter("memberId"))){
            memberId = Integer.valueOf(request.getParameter("memberId"));
        }
        String giftState = request.getParameter("giftState");
        ActivitySign signsRecord = new ActivitySign();
        signsRecord.setActivityId(activityId);
        signsRecord.setMemberId(memberId);
        if (!"".equals(giftState)){
            signsRecord.setGiftState(giftState);
        }

        // 需要分页处理时的条件设定
        String start = request.getParameter("start");
        // 改页处理时
        if (start != null && !"".equals(start)) {
            signsRecord.setStart(Integer.valueOf(start));
        }

        List<ActivitySign> activitySignList = activitySignMapper.selectListByCondition(signsRecord);

        signsRecord.setResultList(activitySignList);
        modelAndView.addObject("page", signsRecord);
        return modelAndView;
    }

    /**
     * 核销礼品
     *
     * @param request
     * @return
     */
    @RequestMapping("activity/toModifySign")
    @ArchivesLog(operationDescription = "核销礼品")
    public ModelAndView toModifySign(HttpServletRequest request, Integer signId) {

        ModelAndView modelAndView = new ModelAndView("msg");

        AjaxReturnData ajaxReturnData = activitySignService.reciveGift(signId);

        if (ajaxReturnData.isResult()) {
            modelAndView.addObject("msg", "礼品核销成功");
        } else {
            if ("1".equals(ajaxReturnData.getType())) {
                modelAndView.addObject("msg", "礼品库存不足，请添加奖品后再核销");
            } else {
                modelAndView.addObject("msg", "礼品核销失败");
            }
        }

        modelAndView.addObject("returnUrl", "operation/activity/rewardDetail?" + ajaxReturnData.getUrl());
        return modelAndView;
    }
}